<template>
  <Teleport to="body">
    <div class="modal" :class="{close:!open}" @click="hidden" data-name="modal">
      <div class="content" v-if="open">
        <div class="header">
          <div @click="clear"><i class="iconfont icon-delete"></i> 清空</div>
          <h4>购物车</h4>
        </div>
        <div class="list">
          <cartItem v-for="item in cartStore.goodsList" :key="item.id" :item="item"></cartItem>
        </div>
      </div>
      <div class="cart">
        <div class="left" v-show="!open">
          <div class="btn" @click="goToOrder">
            <div class="pic">
              <img src="@/assets/img/ico_ico_myorder.png" alt="">
            </div>
            <span>订单</span>
          </div>
        </div>
        <div class="right">
          <div class="lef" @click="emits('checked',!open)">
            <div class="pic">
              <img src="@/assets/img/ico_shoppingcart_btn.png" alt="">
            </div>
            <span>购物车</span>
          </div>
          <div class="rig">
            <div class="btn" @click="buy">立即购买</div>
          </div>
        </div>
      </div>
    </div>
  </Teleport>

</template>

<script setup>
import cartItem from './cart-item.vue';
import { useRouter } from 'vue-router';
import { useCartStore } from '@/store/module/cart';

const cartStore = useCartStore()
const router = useRouter()

const {open} = defineProps({
  open:{
    type:Boolean,
    default:false
  }
})

const emits = defineEmits(['checked'])

const hidden = (e)=>{
  if(e.target.dataset.name === 'modal'){
    emits('checked',false)
  }
}

const buy = ()=>{
  router.push('/ordersure')
}

const goToOrder = ()=>{
  router.push('/myorder')
}

const clear= ()=>{
  cartStore.clear()
}

</script>

<style scoped lang="scss">

.modal {
  position: fixed;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .5);
  bottom: 0;
  overflow: hidden;

  .content{
    position: absolute;
    left: 0;
    bottom:84px;
    width: 100%;
    background: #fff;
    overflow: scroll;
    .list{
      height: 300px;
      overflow: scroll;
    }
    .header{
      position: relative;
      text-align: center;
      height: 50px;
      line-height: 50px;
      border-bottom: 1px solid #ddd;
      padding: 0 15px;
      top: 0;
      left: 0;
      div{
        position: absolute;
        top:0;
        left: 15px;
        font-size: 14px;
        color:#999;
      }
      h4{
        font-size: 17px;
        color:#333;
      }
    }
  }

  &.close{
    height: 84px;
  }

  .cart {
    position: fixed;
    left: 0;
    bottom: 0;
    right: 0;
    padding: 15px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .left {
      .btn {
        border: 1px solid #eee;
        border-radius: 50%;
        width: 50px;
        height: 50px;
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: center;

        // justify-content: space-between;
        .pic {
          width: 18px;
          height: 20px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        span {
          font-size: 10px;
          color: #2493F1;
          padding-top: 6px;
          // padding-bottom: 10px;
        }
      }

    }

    .right {
      transition: all 1s;
      flex:1;
      width: 265px;
      padding: 7px 7px 7px 25px !important;
      border-radius: 25px;
      padding: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #2493F1;

      .lef {
        .pic {
          width: 23px;
          height: 24px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        span {
          font-size: 10px;
          color: #fff;
        }
      }

      .rig {
        .btn {
          width: 100px;
          height: 34px;
          background: #fff;
          color: #2493F1;
          border-radius: 17px;
          text-align: center;
          line-height: 34px;
          box-shadow: 0px 2px 12px rgba(36, 147, 241, 0.6);
        }
      }
    }
  }
}
</style>